package com.zf.wms.purview.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zf.wms.purview.entity.Role;
import com.zf.wms.purview.exception.OperateException;
import com.zf.wms.purview.service.RoleService;
import com.zf.wms.purview.utils.ResultMap;
import com.zf.wms.purview.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Author：sl
 * Date：2019-11-04 9:43
 * Description：<描述>
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     * @param roleName
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMap add(@RequestParam("roleName") String roleName) {
        Role role = new Role();
        role.setRoleName(roleName);
        //设置当前时间
        role.setCreateTime(LocalDateTime.now().format(TimeUtil.dateToStrFormatter));

        //此处应从缓存中获取当前用户的名字
        role.setCreateUser("张三");

        roleService.save(role);
        return ResultMap.ok("添加成功功");
    }

    /**
     * 查询所有角色(按编辑时间倒叙)
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMap getList(@RequestParam(value = "pageNum") Integer pageNum,
                             @RequestParam(value = "pageSize") Integer pageSize) {

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc("create_time");

        PageHelper.startPage(pageNum, pageSize);
        List<Role> roles = roleService.list(queryWrapper);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return ResultMap.ok().put("rolesInfo", pageInfo);
    }


    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/removeRoleById", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMap removeRoleById(@RequestParam("roleId") Long roleId) {
        try {
            roleService.deleteById(roleId);
            return ResultMap.ok("删除成功");

        } catch (OperateException e) {
            return ResultMap.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/modifyRoleById", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMap modifyRoleById(@RequestBody Role role) {

        //此处应从缓存中获取当前用户的名字
        role.setUpdateUser("李四");
        //设置当前时间
        role.setUpdateTime(LocalDateTime.now().format(TimeUtil.dateToStrFormatter));
        roleService.updateById(role);
        return ResultMap.ok("更改成功");
    }

    @RequestMapping(value = "/getListByRoleName", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMap getListByRoleName(@RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,@RequestParam(value = "roleName")String roleName) {

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("role_name",roleName);

        PageHelper.startPage(pageNum, pageSize);
        List<Role> roles = roleService.list(queryWrapper);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return ResultMap.ok().put("rolesInfo", pageInfo);
    }
}
